package com.ctn.information.adapter;

import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ctn.information.R;
import com.ctn.information.content.ViewPagerItemView;
import com.ctn.information.entity.SwitchImageInfo;
import com.ctn.information.sycn.AsyncImageLoader;
import com.ctn.information.sycn.ImageCacher;
import com.ctn.information.sycn.AsyncImageLoader.ImageCallback;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ViewPagerAdapter extends PagerAdapter {
    Context context;
    List<SwitchImageInfo> images;
    private HashMap<String, Drawable> mHashMap;
    // 图片状态：1 请求中 2 请求完毕
    private HashMap<String, String> imageStatusMap;

    private AsyncImageLoader asyncImageLoader = null;

    private LayoutInflater mInflater;

    private static ViewPagerAdapter viewPagerAdapter;

    private ViewPagerAdapter(Context context, List<SwitchImageInfo> images) {
        this.context = context;
        this.images = images;
        asyncImageLoader = new AsyncImageLoader(context);
        mHashMap = new HashMap<String, Drawable>();

        imageStatusMap = new HashMap<String, String>();
        this.mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public static synchronized ViewPagerAdapter getIntance(Context context,
            List<SwitchImageInfo> images) {
        if (viewPagerAdapter != null) {
            return viewPagerAdapter;
        }
        viewPagerAdapter = new ViewPagerAdapter(context, images);
        return viewPagerAdapter;

    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        return super.instantiateItem(container, position);
    }

    @Override
    public Object instantiateItem(View arg0, int position) {
        ImageView imageView = new ImageView(context);
        SwitchImageInfo imageInfo = images.get(position);
        final String url = imageInfo.getUrl();
        String target = imageInfo.getTarget();
        String tag = target;
        if(mHashMap.containsKey(url)){
            imageView.setImageDrawable(mHashMap.get(url));
            ((ViewPager) arg0).addView(imageView);
            return imageView;
        }
        if (imageStatusMap.containsKey(url)) {
            if (imageStatusMap.get(url).equals("2")) {
                imageView.setImageDrawable(mHashMap.get(url));
                imageView.setTag(tag);
                imageView.setOnClickListener(new ImageClickListener());
                ((ViewPager) arg0).addView(imageView);
                return imageView;
            } else {
                imageView.setImageResource(R.drawable.favorites);
                ((ViewPager) arg0).addView(imageView);
                return imageView;
            }
        }

        imageStatusMap.put(url, "1");
        Drawable cachedImage = asyncImageLoader.loadDrawable(
                ImageCacher.EnumImageType.Avatar, url, new ImageCallback() {
                    public void imageLoaded(Drawable imageDrawable, String tag) {
                        Log.i("Drawable", tag);
                        mHashMap.put(url, imageDrawable);
                        imageStatusMap.put(url, "2");
                        notifyDataSetChanged();
                    }
                });

        imageView.setImageDrawable(cachedImage);
        ((ViewPager) arg0).addView(imageView);
        return imageView;
    }

    public class ImageClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String tag = (String)v.getTag();
            System.out.println(tag);
        }
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {

    }

    @Override
    public void finishUpdate(View arg0) {

    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}
